<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

          <p-col :span="12">
            <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productName', validatorRules.productName]" placeholder="请输入产品名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="制令类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="product_type" placeholder="请选择制令类型"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="product_status" placeholder="请选择状态"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="车间名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'workName', validatorRules.workName]" placeholder="请输入车间名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="预计开工日" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择预计开工日" v-decorator="[ 'startDate', validatorRules.startDate]" :trigger-change="true" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="预计完工日" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择预计完工日" v-decorator="[ 'endDate', validatorRules.endDate]" :trigger-change="true" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="物料需求" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="fxMaterialRequireTable.loading"
            :columns="fxMaterialRequireTable.columns"
            :dataSource="fxMaterialRequireTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
        <p-tab-pane tab="物料供应商" :key="refKeys[1]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[1]"
            :loading="fxMaterialProviderTable.loading"
            :columns="fxMaterialProviderTable.columns"
            :dataSource="fxMaterialProviderTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'FxProductionOrdersModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          productName: {rules: [
          ]},
          type: {rules: [
          ]},
          status: {rules: [
            {required: true, message: '请输入状态!'},
          ]},
          workName: {rules: [
          ]},
          startDate: {rules: [
          ]},
          endDate: {rules: [
          ]},
          remark: {rules: [
          ]},
        },
        refKeys: ['fxMaterialRequire', 'fxMaterialProvider', ],
        tableKeys:['fxMaterialRequire', 'fxMaterialProvider', ],
        activeKey: 'fxMaterialRequire',
        // 物料需求
        fxMaterialRequireTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '物料编号',
              key: 'materialNo',
              type: FormTypes.popup,
              popupCode: 'material_info',
              destFields: 'materialNo,materialName,unit',
              orgFieldse: 'material_no,material_name,unit',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料名称',
              key: 'materialName',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '单位',
              key: 'unit',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },



          ]
        },
        // 物料供应商
        fxMaterialProviderTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '供应商名称',
              key: 'providerName',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '供应商地址',
              key: 'providerAddr',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '数量',
              key: 'number',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '联系人',
              key: 'contact',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '联系电话',
              key: 'contactPhone',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: '/codedemoapi/feixuns/fxProductionOrders/add',
          edit: '/codedemoapi/feixuns/fxProductionOrders/edit',
          fxMaterialRequire: {
            list: '/codedemoapi/feixuns/fxProductionOrders/queryFxMaterialRequireByMainId'
          },
          fxMaterialProvider: {
            list: '/codedemoapi/feixuns/fxProductionOrders/queryFxMaterialProviderByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'createTime','productName','type','status','workName','startDate','endDate','remark')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.fxMaterialRequire.list, params, this.fxMaterialRequireTable)
          this.requestSubTableData(this.url.fxMaterialProvider.list, params, this.fxMaterialProviderTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          fxMaterialRequireList: allValues.tablesValue[0].values,
          fxMaterialProviderList: allValues.tablesValue[1].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'createTime','productName','type','status','workName','startDate','endDate','remark'))
     },

    }
  }
</script>

<style scoped>
</style>